﻿using AutoMapper;
using MediatR;
using ZGLXM.ErrorCode;
using ZGLXM.Infra.Interface;
using ZGLXM.Read.Api.Applicantion.Command.RBAC;

namespace ZGLXM.Read.Api.Applicantion.Handler.RBAC
{
    public class RoleByIdCommandHandler : IRequestHandler<RoleByIdCommand, ApiResult>
    {
        private readonly IRoleRepository roleRepository;
        private readonly IMapper mapper;
        private readonly ILogger<RoleByIdCommandHandler> logger;
        public RoleByIdCommandHandler(IRoleRepository roleRepository, IMapper mapper, ILogger<RoleByIdCommandHandler> logger)
        {
            this.roleRepository = roleRepository;
            this.mapper = mapper;
            this.logger = logger;
        }

        public async Task<ApiResult> Handle(RoleByIdCommand request, CancellationToken cancellationToken)
        {
            var role = await roleRepository.GetAsync(x => x.IsDeleted == false && x.RoleId == request.RoleId);
            if (role == null)
            {
                return ApiResult.Fail("角色不存在");
            }
            return ApiResult.OK(role);
        }
    }
}
